package com.gongpingjialoan.activity.car;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.OrderCheckCarListAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.OrderCarData;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.widget.LoadingDialog;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 预约验车
 * huangcg
 */
public class OrderCheckCarActivity extends BaseActivity {

    private ListView order_info_list;
    private TextView phone_tv;
    private OrderCheckCarListAdapter mOrderCheckCarListAdapter;
    private List<OrderCarData.DataBean> databean = new ArrayList<>();

    private NetDataJson mNetDataJson;
    private NetDataJson mAppointNetDataJson;
    private Dialog mLoadingDialog;

    private Intent intent;

    private String mCity;
    private String mPhone;
    private String car_id;
    private String validation_partner;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        isSendData = true;
        isScrollPage = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_car);
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        if (mNetDataJson != null) {
            mNetDataJson.cancelTask();
        }
        if (mAppointNetDataJson != null) {
            mAppointNetDataJson.cancelTask();
        }
        super.onDestroy();
    }

    private void initData() {
        intent = getIntent();
        mCity = intent.getStringExtra("city");
        mPhone = intent.getStringExtra("phone");
        car_id = intent.getStringExtra("car_id");
        phone_tv.setText(mPhone);
        mLoadingDialog = new LoadingDialog(this);
        getSupportedValidationPartners();

    }

    private void initView() {
        order_info_list = (ListView) findViewById(R.id.order_info_list);
        phone_tv = (TextView) findViewById(R.id.phone_tv);

        findViewById(R.id.order_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orderRequest();
            }
        });
    }

    private void orderRequest() {
        mAppointNetDataJson = new NetDataJson(new NetDataJson.OnStringListener() {

            @Override
            public void onDataJsonUpdate(String json) {
                mLoadingDialog.dismiss();
                Toast.makeText(OrderCheckCarActivity.this, "预约成功", Toast.LENGTH_SHORT).show();
                Intent it = getIntent();
                setResult(Activity.RESULT_OK, it);
                finish();

            }

            @Override
            public void onDataJsonError(String errorMessage) {
                mLoadingDialog.dismiss();
                Toast.makeText(OrderCheckCarActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }

        }, 1);
        mLoadingDialog.show();

        mAppointNetDataJson.addParam("phone", mPhone);
        mAppointNetDataJson.addParam("car_id", car_id);
        mAppointNetDataJson.addParam("validation_partner", databean.get(mOrderCheckCarListAdapter.getSelectPosition()).getName());
        mAppointNetDataJson.setUrl(API.post_add_validate_appoint);
        mAppointNetDataJson.request("post");

    }


    private void getSupportedValidationPartners() {
        mNetDataJson = new NetDataJson(new NetDataJson.OnStringListener() {

            @Override
            public void onDataJsonUpdate(String json) {
                mLoadingDialog.dismiss();
                Gson gson = new Gson();
                OrderCarData orderCarData = gson.fromJson(json, OrderCarData.class);
                databean = orderCarData.getData();
                mOrderCheckCarListAdapter = new OrderCheckCarListAdapter(databean, OrderCheckCarActivity.this);
                order_info_list.setAdapter(mOrderCheckCarListAdapter);

            }

            @Override
            public void onDataJsonError(String errorMessage) {
                mLoadingDialog.dismiss();
            }

        }, 1);
        mLoadingDialog.show();

        mNetDataJson.addParam("city", mCity);
        mNetDataJson.setUrl(API.get_supported_validation_partners);
        mNetDataJson.request("get");
    }
}
